package com.kly.order.controller;

import com.alibaba.dubbo.rpc.support.RpcUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Response;
import com.kly.dto.MemberDto;
import com.kly.dto.ReduceBenefitsDto;
import com.kly.enums.PayGatewayEnum;
import com.kly.order.db.dao.MemberDao;
import com.kly.order.db.entity.MemberEntity;
import com.kly.order.mapper.MemberMapper;
import com.kly.order.serviceImpl.MemberServiceImpl;
import com.kly.order.serviceImpl.ProductCategoryServiceImpl;
import com.kly.order.serviceImpl.ProductServiceImpl;
import com.kly.order.serviceImpl.UserVasPurchasesServiceImpl;
import com.kly.order.task.GenerateBenefitTask;
import com.kly.pay.task.RetrySubscriptionTask;
import com.kly.req.RepayRequest;
import com.kly.req.UserConsumeRequest;
import com.kly.service.pay.RepayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OKController {
    @Autowired
    MemberMapper mapper;

    @Autowired
    MemberDao memberDao;

    @Autowired
    ProductServiceImpl productService;

    @Autowired
    ProductCategoryServiceImpl productCategory;

    @Resource
    MemberServiceImpl memberService;
    @Resource
    GenerateBenefitTask generateBenefitTask;
    @Resource
    RetrySubscriptionTask retrySubscriptionTask;

    @Resource
    UserVasPurchasesServiceImpl userVasPurchasesService;

    @RequestMapping(value = "/01")
    public MemberDto test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setMemberType(1);
        memberEntity.setMemberCode("123");
        MemberDto memberDto = mapper.toDto(memberEntity);
        return memberDto;
    }

    @RequestMapping(value = "/ok")
    public String ok() {
//        generateBenefitTask.execute();
        return "ok";
    }

    @RequestMapping(value = "/generateBenefitTask")
    public String generateBenefitTask() {
        generateBenefitTask.execute();
        return "ok";
    }

    @RequestMapping(value = "/02")
    public MemberDto test2(String code) {
        Date date = new Date();
        Map<String, Object> map = new HashMap<>();
        map.put("userCode", code);
        map.put("date", date);
        List<MemberEntity> memberEntities = memberDao.selectByUser(map);
        if (memberEntities != null) {
            MemberEntity memberEntity = memberEntities.get(0);
            Date effectiveDate = memberEntity.getEffectiveDate();
            Date expiryDate = memberEntity.getExpiryDate();
            long l = expiryDate.getTime() - effectiveDate.getTime();

        }
        System.out.println(JSON.toJSON(memberEntities));
        return null;
    }

    @RequestMapping(value = "/03")
    public void test03() {
        productService.queryProductsByProductCategory("sparksy", null);
    }

    @RequestMapping(value = "/04")
    public Response test04(int a) {
        return productCategory.queryProductCategory("sparksy", a);
    }

    @RequestMapping(value = "/05")
    public Response test05(String userCode) {
        return memberService.queryMemberSubscription("sparksy", userCode,1);
    }


    @RequestMapping(value = "/06")
    public Response test06(@RequestBody ReduceBenefitsDto reduceBenefitsDto) {
        return userVasPurchasesService.reduceUserBenefits(reduceBenefitsDto);
    }


    @RequestMapping(value = "/07")
    public String test07() {
         retrySubscriptionTask.execute();
         return "ok";
    }

    public static void main(String[] args) {
    }

    @RequestMapping(value = "/testConsume")
    public Response testConsume(@RequestBody UserConsumeRequest request) {
        return Response.ok(userVasPurchasesService.getUserConsumptionRecord(request));
    }

    @Resource
    RepayService repayService;
    @RequestMapping(value = "/queryExtentInfo")
    public String queryExtentInfo() {
        JSONObject extJson = new JSONObject();
        extJson.put("packageName", "com.kly.meboo");
        extJson.put("subscriptionId", "meboo_subscription_101");
        extJson.put("purchaseToken", "ggdcienhclbkldjeipggphid.AO-J1OyMHMksH1w13Ur2aVHmwVTXRuIa_JWVxKeX1jmESlKv6NR2Dcxo0kry4w6m1aFGK1ZY-reno6zp5NyeOgEJGw8zXdhL_g");
        final RepayRequest repayRequest = RepayRequest.builder().payExt(extJson.toJSONString()).payGateway(PayGatewayEnum.GOOGLE_PAY_SUBSCRIPTIONS.getCode()).build();
        return JSONObject.toJSONString(repayService.queryExtentInfo(repayRequest));
    }



}
